package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.EvaluationsMapper;
import com.ruoyi.system.domain.Evaluations;
import com.ruoyi.system.service.IEvaluationsService;

/**
 * 评论信息Service业务层处理
 * 
 * @author 张英旺
 * @date 2024-02-27
 */
@Service
public class EvaluationsServiceImpl implements IEvaluationsService 
{
    @Autowired
    private EvaluationsMapper evaluationsMapper;

    /**
     * 查询评论信息
     * 
     * @param evaluationId 评论信息主键
     * @return 评论信息
     */
    @Override
    public Evaluations selectEvaluationsByEvaluationId(Long evaluationId)
    {
        return evaluationsMapper.selectEvaluationsByEvaluationId(evaluationId);
    }

    /**
     * 查询评论信息列表
     * 
     * @param evaluations 评论信息
     * @return 评论信息
     */
    @Override
    public List<Evaluations> selectEvaluationsList(Evaluations evaluations)
    {
        return evaluationsMapper.selectEvaluationsList(evaluations);
    }

    /**
     * 新增评论信息
     * 
     * @param evaluations 评论信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEvaluations(Evaluations evaluations)
    {
        evaluations.setCreateTime(DateUtils.getNowDate());
        int rows = evaluationsMapper.insertEvaluations(evaluations);
        insertSysUser(evaluations);
        return rows;
    }

    /**
     * 修改评论信息
     * 
     * @param evaluations 评论信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEvaluations(Evaluations evaluations)
    {
        evaluations.setUpdateTime(DateUtils.getNowDate());
        evaluationsMapper.deleteSysUserByUserId(evaluations.getEvaluationId());
        insertSysUser(evaluations);
        return evaluationsMapper.updateEvaluations(evaluations);
    }

    /**
     * 批量删除评论信息
     * 
     * @param evaluationIds 需要删除的评论信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEvaluationsByEvaluationIds(Long[] evaluationIds)
    {
        evaluationsMapper.deleteSysUserByUserIds(evaluationIds);
        return evaluationsMapper.deleteEvaluationsByEvaluationIds(evaluationIds);
    }

    /**
     * 删除评论信息信息
     * 
     * @param evaluationId 评论信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEvaluationsByEvaluationId(Long evaluationId)
    {
        evaluationsMapper.deleteSysUserByUserId(evaluationId);
        return evaluationsMapper.deleteEvaluationsByEvaluationId(evaluationId);
    }

    /**
     * 新增用户信息信息
     * 
     * @param evaluations 评论信息对象
     */
    public void insertSysUser(Evaluations evaluations)
    {
        List<SysUser> sysUserList = evaluations.getSysUserList();
        Long evaluationId = evaluations.getEvaluationId();
        if (StringUtils.isNotNull(sysUserList))
        {
            List<SysUser> list = new ArrayList<SysUser>();
            for (SysUser sysUser : sysUserList)
            {
                sysUser.setUserId(evaluationId);
                list.add(sysUser);
            }
            if (list.size() > 0)
            {
                evaluationsMapper.batchSysUser(list);
            }
        }
    }
}
